﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Models;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Controllers.Global;

namespace WebHoiChoMuaBan.Controllers
{
    public class LoginController : PublicController
    {
        //
        // GET: /Register/

        public ActionResult Index()
        {

            return View("~/Views/Home/Login.aspx");
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View("~/Views/Home/Login.aspx");
        }

        [HttpPost]
        public ActionResult Login(FormCollection collection)
        {
            string usname = collection.Get("Username");
            string pass = collection.Get("Password");
            bool isOK = true;
            if (string.IsNullOrEmpty(usname))
            {
                isOK = false;
                ModelState.AddModelError("Username", "Hãy nhập tên đăng nhập.");
            }
            if (string.IsNullOrEmpty(pass))
            {
                isOK = false;
                ModelState.AddModelError("Password", "Hãy nhập mật khẩu.");
            }

            if (isOK)
            {
                MemberHelper hMem = new MemberHelper();
                Member mem = hMem.Login(usname, pass);
                if (mem != null)
                {
                    //copy saved NewsProduct
                    string savedNews = Session["MemberSavedNewsProduct"] + "";
                    string[] newsid = savedNews.Split('@');
                    NewsProductHelper proHelper = new NewsProductHelper();
                    Boolean isSuccess;
                    for (int i = 0; i < newsid.Length; i++)
                    {
                        if (!newsid[i].Equals(""))
                          isSuccess =  proHelper.SaveMemberSaveNewsProduct(mem.MemId.ToString(), newsid[i]);
                    }
                    Session.Remove("MemberSavedNewsProduct");

                    Session["IsLogin"] = true;
                    Session["isEditMode"] = true;
                    Session["MemId"] = mem.MemId;
                    Session["MemUserName"] = mem.MemUsername;
                    Session["MemUserEmail"] = mem.MemEmail;
                    switch (mem.MemTypeId)
                    {
                        case 6:
                            {
                                String rights = "1,2,3,4,5,6,7";
                                Session["Rights"] = rights;
                                break;
                            }
                        case 7:
                            {
                                String rights = "1, 2, 3, 4 ,6";
                                Session["Rights"] = rights;
                                break;
                            }
                        case 8:
                            {
                                String rights = "3, 4, 6";
                                Session["Rights"] = rights;
                                break;
                            }
                        default:
                            break;
                    }
                    if (mem.MemTypeId >= 6)
                        return RedirectToAction("Index", "AdminHome");
                    else
                        Response.Redirect("/");
                }
                else
                    ModelState.AddModelError("Password", "Tên đăng nhập hoặc nhập mật khẩu không đúng.");
            }

            return Index();
        }

        public ActionResult Logout()
        {
            Session.RemoveAll();
            Session.Abandon();
            Response.Redirect("/");
            return null;
        }
    }
}
